﻿Imports BUS
Imports DAO
Imports DTO
Imports System.Linq
Imports System.Text.RegularExpressions

Public Class TraCuuHS
    Public Shared Function IsEmail(ByVal Email As String) As Boolean
        Dim strEmail As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" & "\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" & ".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"

        Dim re As New Regex(strEmail)
        If re.IsMatch(Email) Then
            Return (True)
        Else
            Return (False)
        End If
    End Function

    Public Function IsBorn(ByVal born As Date) As Boolean
        If ((Date.Today.Year - date_date.Value.Year > 20) Or (Date.Today.Year - date_date.Value.Year < 15)) Then
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub btn_Thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Thoat.Click
        Close()
    End Sub

    Private Sub cbx_khoi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_khoi.SelectedIndexChanged
        Dim temp As Integer
        Dim k As KhoiDTO = cbx_khoi.SelectedValue
        temp = k.MaKhoi

        Dim lop As LopBUS = New LopBUS()
        cbx_lop.DisplayMember = "TenLop"
        cbx_lop.ValueMember = "MaL"

        If temp = 0 Then
            Dim list As List(Of LopDTO) = New List(Of LopDTO)
            Dim l3 As New LopDTO
            l3.MaLop = ""
            l3.TenLop = "--- Tất cả ---"

            list.Add(l3)

            For Each k2 As KhoiDTO In cbx_khoi.Items
                If k2.MaKhoi <> 0 Then
                    Dim list2 As List(Of LopDTO) = lop.getClass_BUS(k2.MaKhoi)
                    For Each l As LopDTO In list2
                        list.Add(l)
                    Next
                End If
            Next

            cbx_lop.DataSource = list
        Else
            cbx_lop.DataSource = lop.getClass_BUS(temp)
        End If


        lbl_error.Text = ""
    End Sub

    Private Sub cbx_year_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_year.SelectedIndexChanged
        lbl_error.Text = ""
    End Sub

    Private Sub cbx_lop_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_lop.SelectedIndexChanged
        lbl_error.Text = ""

        'Dim temp As String
        'Dim lop As LopDTO = cbx_lop.SelectedValue
        'temp = lop.MaLop

        'Dim khoi As KhoiBUS = New KhoiBUS()
        'cbx_khoi.DisplayMember = "TenKhoi"
        'cbx_khoi.ValueMember = "MaK"

        'cbx_khoi.DataSource = khoi.getKhoitheoLop_BUS(temp)
    End Sub

    Private Sub TraCuuHS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        date_date.Format = DateTimePickerFormat.Custom
        date_date.CustomFormat = "dd/MM/yyyy"

        Dim kBus As KhoiBUS = New KhoiBUS()
        cbx_khoi.DisplayMember = "TenKhoi"
        cbx_khoi.ValueMember = "MaK"

        Dim list As List(Of KhoiDTO) = kBus.getLevel_BUS()
        Dim khoidto As New KhoiDTO
        khoidto.MaKhoi = 0
        khoidto.TenKhoi = "--- Tất cả ---"

        list.Insert(0, khoidto)

        cbx_khoi.DataSource = list

        Dim temp As Integer
        Dim k As KhoiDTO = cbx_khoi.SelectedValue
        temp = k.MaKhoi

        Dim yBUS As HocKyBUS = New HocKyBUS()
        cbx_year.DisplayMember = "NamHoc"
        cbx_year.ValueMember = "NamHoc"
        cbx_year.DataSource = yBUS.getYear_BUS()

        Dim ttBus As TinhTrangBUS = New TinhTrangBUS()
        cbx_tinhtrang.DisplayMember = "LoaiTT"
        cbx_tinhtrang.ValueMember = "MaTT"

        Dim listtt As List(Of TinhTrangDTO) = ttBus.getTinhTrang_BUS()
        Dim ttDto As New TinhTrangDTO
        ttDto.MaTT = 0
        ttDto.LoaiTT = "--- Tất cả ---"
        listtt.Insert(0, ttDto)
        cbx_tinhtrang.DataSource = listtt
    End Sub

    Private Sub btn_tracuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_tracuu.Click
        Dim kDto As KhoiDTO = cbx_khoi.SelectedValue
        Dim mak As Integer = kDto.MaKhoi

        Dim lDto As LopDTO = cbx_lop.SelectedValue
        Dim mal As String = lDto.MaLop

        Dim matt As Integer = cbx_tinhtrang.SelectedValue
        Dim stname As String = txt_name.Text
        Dim address As String = txt_address.Text
        Dim email As String = txt_email.Text
        Dim borndate As Date = date_date.Value.Date
        Dim sex As Integer
        If rd_nam.Checked = True Then
            sex = 1
        ElseIf rd_nu.Checked = True Then
            sex = 2
        Else
            sex = 0
        End If

        Dim year As Integer = cbx_year.SelectedValue

        Dim pfBus As HoSoHSBUS = New HoSoHSBUS()
        dgv_listst.DataSource = pfBus.findStudent_BUS(stname, address, email, borndate, sex, mak, mal, year, matt)

        Dim stt As Integer = 0
        For Each row As DataGridViewRow In dgv_listst.Rows
            row.Cells(0).Value = stt + 1
            stt = stt + 1
        Next

        If Me.dgv_listst.RowCount = 0 Then
            lbl_error.Text = "Không tìm thấy học sinh"
        Else
            lbl_error.Text = ""
        End If
    End Sub

    Private Sub txt_name_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_name.KeyPress
        lbl_error.Text = ""
        lbl_name.Text = ""
    End Sub

    Private Sub btn_update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Dim mahs As Integer = Me.dgv_listst.Item(1, dgv_listst.CurrentRow.Index).Value.ToString()
        'Dim hsname As String = txt_name.Text

        'Dim kDto As KhoiDTO = cbx_khoi.SelectedValue
        'Dim mak As Integer = kDto.MaKhoi

        'Dim lDto As LopDTO = cbx_lop.SelectedValue
        'Dim mal As String = lDto.MaLop

        'Dim matt As Integer = cbx_tinhtrang.SelectedValue
        'Dim hosoBus As HoSoHSBUS = New HoSoHSBUS()
        'hosoBus.updateProfileStudent_BUS(mahs, hsname, mak, mal, matt)

        'MessageBox.Show("sdddddddd")
    End Sub

    Private Sub dgv_listst_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv_listst.SelectionChanged
        'txt_name.Text = Me.dgv_listst.Item(2, dgv_listst.CurrentRow.Index).Value.ToString()
        'cbx_lop.DisplayMember = Me.dgv_listst.Item(3, dgv_listst.CurrentRow.Index).Value.ToString()
    End Sub

    Private Sub txt_name_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_name.Leave
        'If (txt_name.Text.Trim() = "") Then
        '    lbl_name.Text = "Tên không được trống!!!"
        '    txt_name.Focus()
        'End If
    End Sub

    Private Sub txt_address_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_address.Leave
        'If (txt_address.Text.Trim() = "") Then
        '    lbl_address.Text = "Thông tin bắt buộc"
        '    txt_address.Focus()
        'End If
    End Sub

    Private Sub txt_email_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_email.Leave
        'If (txt_email.Text.Trim() = "") Then
        '    lbl_email.Text = "Thông tin bắt buộc"
        '    txt_email.Focus()
        'ElseIf (IsEmail(txt_email.Text.Trim()) = False) Then
        '    lbl_email.Text = "Email không hợp lệ"
        '    txt_email.Focus()
        '    Return
        'Else
        '    lbl_email.Text = "Email hợp lệ"
        'End If
    End Sub

    Private Sub date_date_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles date_date.Leave
        'If (Me.IsBorn(date_date.Value) = False) Then
        '    lbl_date.Text = "Học sinh phải từ 15 tới 20 tuổi"
        '    date_date.Focus()
        'End If
    End Sub

    Private Sub txt_address_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_address.KeyPress
        lbl_address.Text = ""
    End Sub

    Private Sub txt_email_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_email.KeyPress
        lbl_email.Text = ""
    End Sub

    Private Sub date_date_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles date_date.KeyPress
        lbl_date.Text = ""
    End Sub

    Private Sub cbx_tinhtrang_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_tinhtrang.SelectedIndexChanged
        'lbl_tt.Text = ""
    End Sub

    Private Sub btn_reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_reset.Click
        txt_email.Text = ""
        txt_address.Text = ""
        txt_name.Text = ""
        date_date.Value = Date.Today()
    End Sub

    Private Sub dgv_listst_ColumnHeaderMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv_listst.ColumnHeaderMouseClick
        Dim col As DataGridViewColumn
        For Each col In dgv_listst.Columns
            col.SortMode = DataGridViewColumnSortMode.NotSortable
        Next
    End Sub

End Class